package leave.model.impl.mysql;

import leave.dataaccess.EmployeeLeaveDAO;
import leave.dataaccess.mysql.CompensationLeaveLedgerDAOImpl;
import leave.dataaccess.mysql.LeaveDAOImpl;
import leave.exception.DataAccessException;
import leave.model.CompensationLeaveLedger;
import leave.model.Employee;
import leave.model.Leave;
import leave.model.Manager;

import java.sql.Connection;
import java.util.ArrayList;

public class ManagerImpl extends EmployeeImpl implements Manager {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5027007507388120233L;
	Connection factoryConnection = null;
	private ArrayList<Integer> subordinate;

	public ManagerImpl() throws DataAccessException {
		super();
		subordinate = new ArrayList<Integer>();
	}

	public ManagerImpl(Connection con) throws DataAccessException {
		super();
		factoryConnection = con;
	}

	@Override
	public Employee[] getSubordinate() throws DataAccessException {
		// TODO Auto-generated method stub
		ArrayList<Employee> subordinateList = new ArrayList<Employee>();
		EmployeeLeaveDAO EmployeeAccess = DAOF.getEmployeeLeaveDAO();
		for (int i : subordinate) {
			subordinateList.add(EmployeeAccess.getEmployee(i));
		}
		return (Employee[]) subordinateList.toArray();
	}

	public void setSubordinate(ArrayList<Integer> subordinate) {
		this.subordinate = subordinate;
	}

	@Override
	public void addSubordinate(Employee subordinate) throws DataAccessException {
		// TODO Auto-generated method stub
		EmployeeLeaveDAO EmployeeAccess = DAOF.getEmployeeLeaveDAO();
		EmployeeAccess.insertEmployee(subordinate);
	}

	@Override
	public Leave[] getPendingLeave() {
		// TODO Auto-generated method stub
		ArrayList<Leave> leaveList = new ArrayList<Leave>();
		try {
			Employee[] subordinateList = getSubordinate();
			for (Employee e : subordinateList) {
				Leave[] leaveArry = new LeaveDAOImpl(factoryConnection)
						.getPendingLeaveTransactions(e);
				for (Leave l : leaveArry) {
					leaveList.add(l);
				}
			}

		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Leave[]) leaveList.toArray();
	}

	@Override
	public CompensationLeaveLedger[] getPendingLedger() {
		// TODO Auto-generated method stub
		ArrayList<CompensationLeaveLedger> ledgerList = new ArrayList<CompensationLeaveLedger>();
		try {
			Employee[] subordinateList = getSubordinate();
			for (Employee e : subordinateList) {
				CompensationLeaveLedger[] leaveLedgers = new CompensationLeaveLedgerDAOImpl(
						factoryConnection).getPendingLedger(e);
				for (CompensationLeaveLedger c : leaveLedgers) {
					ledgerList.add(c);
				}

			}
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (CompensationLeaveLedger[]) ledgerList.toArray();
	}
}
